package com.cnap.controller;

import com.cnap.model.common.CommonResult;
import com.cnap.model.stage.SingleStageDeployParam;
import com.cnap.model.stage.StageDeployParam;
import com.cnap.model.stage.StageVO;
import com.cnap.service.DistributeDeployService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 阶段控制类
 *
 * @author xc
 **/
@Api(tags = "阶段管理")
@RestController
@RequestMapping("/api/stage")
public class StageController {

    /**
     * 自动化分4个阶段
     * 1. 开通云主机
     * 2. 开通安全组端口
     * 3. k8s部署、纳管、组件安装
     * 4. 微服务部署
     * <p>
     * 1、2、3与集群管理子系统对接，由对方保证3步流程
     * 4与算法引擎对接
     * 提供状态回调接口，更新各个阶段状态
     * 由额外信息时，让如extraInfo字段中
     */

    private static final Logger LOGGER = LoggerFactory.getLogger(StageController.class);
    private final DistributeDeployService distributeDeployService;

    @Autowired
    public StageController(DistributeDeployService distributeDeployService) {
        this.distributeDeployService = distributeDeployService;
    }

    /**
     * 部署
     *
     * @param deployParam 参数
     * @return 结果
     */
    @ApiOperation(value = "部署", notes = "部署", httpMethod = "POST")
    @PostMapping("/deploy")
    public CommonResult<String> startDeploy(@RequestBody StageDeployParam deployParam) {
        LOGGER.info("[stage]: start deploy, svcIds={}",
                deployParam.getParams().stream().map(SingleStageDeployParam::getSvcId).collect(Collectors.toList()));
        distributeDeployService.startDistributeDeploy(deployParam);

        return CommonResult.success("success");
    }

    /**
     * 部署
     *
     * @param svcIds 微服务id列表
     * @return 结果
     */
    @ApiOperation(value = "部署", notes = "部署", httpMethod = "POST")
    @PostMapping("/v2/deploy")
    public CommonResult<String> startDeployV2(@RequestBody List<String> svcIds) {
        LOGGER.info("[stage]: start deploy, svcIds={}", svcIds);
        distributeDeployService.startDistributeDeploy(svcIds);

        return CommonResult.success("success");
    }

    /**
     * 根据微服务id查询阶段信息
     *
     * @param svcId 服务id
     * @return 结果
     */
    @ApiOperation(value = "根据微服务id查询阶段信息", notes = "根据微服务id查询阶段信息", httpMethod = "GET")
    @GetMapping("/service/{svcId}")
    public CommonResult<StageVO> getStageByServiceId(@PathVariable("svcId") String svcId) {
        StageVO stageVO = distributeDeployService.getStageInfoByServiceId(svcId);
        return CommonResult.success(stageVO);
    }

    /**
     * 根据应用id查询阶段信息
     *
     * @param appId 应用id
     * @return 结果
     */
    @ApiOperation(value = "根据应用id查询阶段信息", notes = "根据应用id查询阶段信息", httpMethod = "GET")
    @GetMapping("/app/{appId}")
    public CommonResult<List<StageVO>> getStagesByAppId(@PathVariable("appId") String appId) {
        List<StageVO> stageVOS = distributeDeployService.getStagesInfoByAppId(appId);
        return CommonResult.success(stageVOS);
    }

    /**
     * 重试
     *
     * @param svcId 服务id
     * @return 结果
     */
    @ApiOperation(value = "重试", notes = "重试", httpMethod = "PUT")
    @PutMapping("/retry/service/{svcId}")
    public CommonResult<String> retryWithStage(@PathVariable("svcId") String svcId) {
        LOGGER.info("start retry for service, id={}", svcId);
        distributeDeployService.retryByServiceId(svcId);

        return CommonResult.success("success");
    }


}
